﻿#region [===== Using =====]
using System;
using System.ComponentModel;
#endregion

namespace TPG.SharePoint.Workflow
{
    public partial class TaskData
    {
        /// <summary>
        /// Provides callers of the TaskData class with a 
        /// <see cref="ICustomTypeDescriptor" /> to allow data-binding
        /// against the task data.
        /// </summary>
        public class TaskDataDescriptionProvider
            : TypeDescriptionProvider
        {
            #region [===== Fields =====]
            TypeDescriptionProvider _baseProvider = null;
            #endregion

            #region [===== Constructors =====]
            /// <summary>
            /// Initializes a new instance of the 
            /// <see cref="TaskDataDescriptionProvider" /> class.
            /// </summary>
            public TaskDataDescriptionProvider()
            {
                _baseProvider = TypeDescriptor.GetProvider(
                    typeof(TaskData));
            }
            #endregion

            #region [===== Public methods =====]
            /// <summary>
            /// Creates a <see cref="ICustomTypeDescriptor" /> which provides 
            /// access to the data stored in a TaskData class using Reflection.
            /// </summary>
            /// <param name="objectType">The <see cref="Type"/> of object to
            /// create the descriptor for.</param>
            /// <param name="instance">The instance of an object to create
            /// the descriptor for.</param>
            /// <returns>The custom type descriptor.</returns>
            public override ICustomTypeDescriptor GetTypeDescriptor(
                Type objectType, object instance)
            {
                TaskData taskData = instance as TaskData;
                if (taskData == null)
                {
                    throw new ArgumentException("instance not a TaskData object");
                }
                return new TaskDataDescriptor(
                    _baseProvider.GetTypeDescriptor(objectType, instance),
                    taskData);
            }
            #endregion
        }
    }
}